package com.gin.kotlin.no_gay_alliance.database.entity

import androidx.room.Entity
import androidx.room.PrimaryKey
import com.gin.kotlin.no_gay_alliance.model.emoji.GenericEmojiModel
import java.time.ZonedDateTime

/**
 * 表情使用历史
 * @property code 代码
 * @property url 地址
 * @property count 使用次数
 * @property timestamp 最后一次使用
 * @constructor
 */
@Entity
data class EmojiHistory(
    @PrimaryKey
    var code: String,
    var url: String? = null,
    var count: Int? = null,
    var timestamp: ZonedDateTime? = null,
) {
    /**
     * 次数自增，并更新最后一次使用时间
     * @return EmojiHistory
     */
    fun plus() = this.let {
        this.copy(count = count?.plus(1), timestamp = ZonedDateTime.now())
    }

    /**
     * 初始化
     * @param model GenericEmojiModel
     * @constructor
     */
    constructor(model: GenericEmojiModel) : this(
        code = model.code!!,
        url = model.url,
        count = 0,
        timestamp = ZonedDateTime.now()
    )

    fun toModel() = GenericEmojiModel(code = code, url = url)
}
